<?php

namespace IDock\Insurance\TAIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\DBIC\DBICEnums;
use IDock\Insurance\TAIC\ACICEnums;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class VehicleModelResponse extends AllTAICResponse
{
    /**
     * @var VehicleModelPrinter $printer
     */
    public $printer = VehicleModelPrinter::class;

    public function printerInit(): Response
    {
        $list = $this->get('vehicleModelList', []);

        foreach ($list as $item) {
            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleModelCode = $item['rbcode'] ?? '';
            $vehiclePrinter->vehicleTypeCode = $item['hyModelCode'] ?? '';
            $vehiclePrinter->vehicleName = $item['vehicleJingyouDto']['vehicleName'] ?? '';
            $vehiclePrinter->brandName = $item['brandName'] ?? '';
            $vehiclePrinter->familyName = $item['vehicleJingyouDto']['familyName'] ?? '';
            $vehiclePrinter->power = '';
            $vehiclePrinter->vehiclePrice = (string)($item['purchasePrice'] ?? '');
            $vehiclePrinter->vehiclePriceTax = (string)($item['purchasePrice'] ?? '');

            if ($item['exhaustCapacity'] ?? '') {
                $vehiclePrinter->vehicleExhaust = (string)(($item['exhaustCapacity'] ?? '') / 1000 ?? 0);
            }
            $vehiclePrinter->vehicleSeat = (string)$item['seatCount'] ?? '';
            $vehiclePrinter->vehicleYear = $item['marketDate'] ?? '';

            if ($item['vehicleTonnage'] ?? '') {
                $vehiclePrinter->vehicleTonnage = (string)round($item['vehicleTonnage'] * 1000 ?? 0,2);
            }

            $vehiclePrinter->wholeWeight = (string)($item['vehicleWeight'] ?? '0');

            //能源类型
            $fuelType = $item['fuelType'] ?? '';
            $vehiclePrinter->energyTypeCode = array_flip(DBICEnums::$energyType)[(string)($fuelType)];
            $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode];

            //进口车标识
            $importFlag = $item['importFlag'] ?? 'I0';
            if ($importFlag == '合资车') {
                $importFlag = 'I2';
            }
            if ($importFlag == '国产车') {
                $importFlag = 'I1';
            }
            if ($importFlag == '进口车') {
                $importFlag = 'I3';
            }

            $vehiclePrinter->vehicleImport = $importFlag;
            $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehiclePrinter->vehicleImport];
            //新能源
            $powerTypeCode = $item['powerTypeCode'] ?? '';
            if (in_array($powerTypeCode, ['D6', 'D8', 'D12'])) {
                $vehiclePrinter->isNewEnergy = true;
            }

            $vehiclePrinter->remark = $item['vehicleStyleDesc'] ?? '';
            $vehiclePrinter->item['actualValue'] = (string)($item['actualValue'] ?? '');
            $vehiclePrinter->item['brandName'] = $item['vehicleJingyouDto']['vehicleName'] ?? '';
            $vehiclePrinter->item['hyModelCode'] = $item['hyModelCode'] ?? '';
            $vehiclePrinter->item['noticeType'] = $item['noticeType'] ?? '';
            $vehiclePrinter->item['vehicleJY'] = $item['vehicleJingyouDto'] ?? '';
            $this->printer->setModelList($vehiclePrinter);
        }

        return $this;
    }
}
